package blocksim.chunking;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * Fixed size data chunking based on [1]:
 * "This approach divides a file into equal-sized chunks"
 * 
 * [1] Samer Al-Kiswany, Matei Ripeanu, Sudharshan Vazhkudai, Abdullah Gharaibeh
 * "stdchk: A Checkpoint Storage System for Desktop Grid Computing," 28th
 * International Conference on Distributed Computing Systems (ICDCS), 2008.
 * 
 * @author marcus
 */
public class FixedSizeChunking implements ChunkingStrategy {

  public static final String NAME = "Fixed";

  private int blockSize;
  
  //private DescriptiveStatistics blSizeSt;

  public FixedSizeChunking(int blockSize) {
    this.blockSize = blockSize;
    //this.blSizeSt = new DescriptiveStatistics();
  }

  @Override
  public List<Long> detectBoundaries(String fileName) throws IOException {
    long fileSize = new File(fileName).length();
    List<Long> boundaries = new LinkedList<Long>();
    long i = 0;
    for (i = blockSize; i < fileSize; i += blockSize) {
      boundaries.add(i);
      //blSizeSt.addValue(blockSize);
    }
    boundaries.add(fileSize);
    //blSizeSt.addValue(blockSize);
    return boundaries;
  }

  @Override
  public String getName() {
    return NAME;
  }

  //@Override
  //public DescriptiveStatistics getBlockSizeStats() {
    //return blSizeSt;
 // }
}